package com.gitee.melin.bee.util;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XmlUtils {

    public static String prettyXml(String xmlString, int indent) {
        return prettyXml(xmlString, indent, false);
    }

    public static String prettyXml(String xmlString, int indent, boolean skipDeclaration) {
        try {
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setIndentSize(indent);
            format.setSuppressDeclaration(skipDeclaration);
            format.setEncoding("UTF-8");

            Document document = DocumentHelper.parseText(xmlString);
            StringWriter sw = new StringWriter();
            XMLWriter writer = new XMLWriter(sw, format);
            writer.write(document);
            return sw.toString();
        } catch (Exception e) {
            throw new RuntimeException("Error occurs when pretty-printing xml:" + e.getMessage(), e);
        }
    }

    public static String addHadooopConfigElements(
            String xmlConfig, Map<String, String> elements, int indent, boolean skipDeclaration, String rootComment) {

        try {
            SAXReader reader = new SAXReader();
            Document read = reader.read(new StringReader(xmlConfig));
            Element root = read.getRootElement();
            if (StringUtils.isNotBlank(rootComment) && !StringUtils.contains(xmlConfig, rootComment)) {
                root.addComment(rootComment);
            }

            elements.forEach((key, value) -> {
                if (!StringUtils.contains(xmlConfig, key)) {
                    Element property = root.addElement("property");
                    Element nameEl = property.addElement("name");
                    nameEl.setText(key);
                    Element valueEl = property.addElement("value");
                    valueEl.setText(value);
                }
            });

            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setIndentSize(indent);
            format.setSuppressDeclaration(skipDeclaration);
            format.setEncoding("UTF-8");

            StringWriter sw = new StringWriter();
            XMLWriter writer = new XMLWriter(sw, format);
            writer.write(read);
            return sw.toString();
        } catch (Exception e) {
            throw new RuntimeException("Error occurs when add elements:" + e.getMessage(), e);
        }
    }

    public static Map<String, String> parseHadooopConfig(String xmlConfig) {
        try {
            Map<String, String> configMap = new LinkedHashMap<>();
            SAXReader reader = new SAXReader();
            Document document = reader.read(new StringReader(xmlConfig));
            Element root = document.getRootElement();

            List<Element> properties = root.elements("property");
            for (Element property : properties) {
                Element nameElement = property.element("name");
                Element valueElement = property.element("value");

                if (nameElement != null && valueElement != null) {
                    String name = nameElement.getTextTrim();
                    String value = valueElement.getTextTrim();

                    if (!name.isEmpty()) {
                        configMap.put(name, value);
                    }
                }
            }

            return configMap;
        } catch (Exception e) {
            throw new RuntimeException("Error occurs when parse xml: " + e.getMessage(), e);
        }
    }
}
